package com.tiantianyuegou.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.R;
import com.tiantianyuegou.adapter.GoodsManageAdapter;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;
import com.tiantianyuegou.model.ProductManageModel;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.NetworkUtils;
import com.tiantianyuegou.util.Tools;
import com.tiantianyuegou.view.MyDialogTextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 商家
 */
public class GoodsManageActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ImageView img_title;
    private TextView text_title;

    private RelativeLayout relative_shoping, relative_warehouse;
    private TextView text_shoping, text_shoping_num, text_warehouse, text_warehouse_num;
    private View view_shoping, view_warehouse;
    public ListView list_goods;
    private GoodsManageAdapter goodsManageAdapter;
    private List<ProductManageModel> goodslist;
    private int id;
    private String TAG = "GoodsManageActivity";
    private LinearLayout mLinear_noData;
    private LoadingDialog mLoadingDialog;
    private String mType;
    private String mSaleSize, mStoreSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_manage);
        Tools.changeSystemBar(this);
        initViews();
    }

    @Override
    public void findViewByIds() {
        text_title = (TextView) findViewById(R.id.text_title);
        img_title = (ImageView) findViewById(R.id.img_title);
        text_title.setText("本地商家");
      /*  relative_shoping = (RelativeLayout) findViewById(R.id.relative_shoping);
        relative_warehouse = (RelativeLayout) findViewById(R.id.relative_warehouse);

        text_shoping = (TextView) findViewById(R.id.text_shoping);
        text_shoping_num = (TextView) findViewById(R.id.text_shoping_num);
        text_warehouse = (TextView) findViewById(R.id.text_warehouse);
        text_warehouse_num = (TextView) findViewById(R.id.text_warehouse_num);

        view_shoping = (View) findViewById(R.id.view_shoping);
        view_warehouse = (View) findViewById(R.id.view_warehouse);

        list_goods = (ListView) findViewById(R.id.list_goods);
        mLinear_noData = (LinearLayout) findViewById(R.id.goods_manage_nodata);*/
    }

    @Override
    public void initListener() {
      /*  img_title.setOnClickListener(this);
        relative_shoping.setOnClickListener(this);
        relative_warehouse.setOnClickListener(this);
        list_goods.setOnItemClickListener(this);*/
    }

    @Override
    public void initData() {
       /* mLoadingDialog = new LoadingDialog(this);
        goodslist = new ArrayList<>();
        goodsManageAdapter = new GoodsManageAdapter(this);
        list_goods.setAdapter(goodsManageAdapter);
        goodsManageAdapter.setManageListener(new GoodsManageAdapter.IManageProductListener() {
            @Override
            public void delete(final ProductManageModel model) {
                MyDialogTextView dialogTextView = new MyDialogTextView(GoodsManageActivity.this, "提示", "确定删除此件商品吗？");
                dialogTextView.show();
                dialogTextView.setDialogCallback(new MyDialogTextView.DialogCallback() {
                    @Override
                    public void dialogOpt() {
                        deleteProduct(model);
                    }
                });
            }

            @Override
            public void edit(ProductManageModel model) {

            }

            @Override
            public void changeStatus(ProductManageModel model) {
                changeProductStatus(model);
            }
        });*/
    }

    @Override
    protected void onResume() {
        super.onResume();
      /*  String type = getIntent().getStringExtra("type");
        if ("1".equals(type)) {
            relative_shoping.callOnClick();
        } else if ("2".equals(type)) {
            relative_warehouse.callOnClick();
        }
        if (type == null || "".equals(type)) {
            relative_shoping.callOnClick();
        }*/
    }

    //商品的上下架
   /* private void changeProductStatus(ProductManageModel model) {
        String type = "";
        if ("1".equals(mType)) {
            type = "1";
            mLoadingDialog.setMsg("正在下架商品...");
        } else {
            type = "2";
            mLoadingDialog.setMsg("正在上架商品...");
        }
        mLoadingDialog.show();
        RequestParams params = new RequestParams();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        params.addBodyParameter("tpId", model.getTpId() + "");
        params.addBodyParameter("password", SPHelper.getPassword());
        params.addBodyParameter("type", type);
        Log.e("TAG", "tmId:" + SPHelper.getTmId() + ":tpId:" + model.getTpId() + ":password:" + SPHelper.getPassword()
                + ":type:" + type);
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.GOODS_TAKENDOWN, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "changeProductStatuc response:" + responseInfo.result);
                if (TextUtils.isEmpty(responseInfo.result)) {
                    showMsg("改变商品状态失败");
                } else {
                    try {
                        JSONObject root = new JSONObject(responseInfo.result);
                        String code = root.getString("code");
                        if ("100".equals(code)) {
                            if ("1".equals(mType)) {
                                showMsg("下架成功");
                                getData("1");
                            } else {
                                getData("2");
                                showMsg("上架成功");
                            }
                        } else {
                            String msg = root.getString("message");
                            showMsg(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        showMsg("解析异常");
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "changeProduct Status failure:" + s);
                showMsg("当前网络不可用,请检查您的网络设置");
            }
        });
    }*/

   /* //删除商品
    private void deleteProduct(final ProductManageModel model) {
        mLoadingDialog.setMsg("正在删除商品");
        mLoadingDialog.show();
        RequestParams params = new RequestParams();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        params.addBodyParameter("tpId", model.getTpId() + "");
        params.addBodyParameter("password", SPHelper.getPassword());
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.DELET_PRODUCT, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "delete product response:" + responseInfo.result);
                if (TextUtils.isEmpty(responseInfo.result)) {
                    showMsg("删除失败");
                } else {
                    try {
                        JSONObject root = new JSONObject(responseInfo.result);
                        String code = root.getString("code");
                        if ("100".equals(code)) {
                            getData(mType);
                        } else {
                            String msg = root.getString("message");
                            showMsg(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        showMsg("解析异常");
                    }

                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "delete product failure:" + s);
                showMsg("当前网络不可用,请检查您的网络设置");
            }
        });
    }*/

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
         /*   case R.id.img_title:
                finish();
                break;*/

          /*  case R.id.relative_shoping:
                text_shoping.setTextColor(Color.RED);
                text_shoping_num.setTextColor(Color.RED);
                view_shoping.setVisibility(View.VISIBLE);
                text_warehouse.setTextColor(Color.BLACK);
                text_warehouse_num.setTextColor(Color.BLACK);
                view_warehouse.setVisibility(View.GONE);
                mType = "1";
                getData(mType);
                break;*/

            /*case R.id.relative_warehouse:

                text_shoping.setTextColor(Color.BLACK);
                text_shoping_num.setTextColor(Color.BLACK);
                view_shoping.setVisibility(View.GONE);
                text_warehouse.setTextColor(Color.RED);
                text_warehouse_num.setTextColor(Color.RED);
                view_warehouse.setVisibility(View.VISIBLE);
                mType = "2";
                getData(mType);
                break;*/
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        id = i;
        Log.e("Tag", "onItemClick");
    }

    //获取数据列表
    private void getData(final String type) {
        mLoadingDialog.setMsg("正在获取数据");
        mLoadingDialog.show();
        if (!NetworkUtils.isNetworkAvailable(this)) {
            showMsg("当前网络不可用,请检查您的网络设置");
            return;
        }
        goodslist.clear();
        RequestParams params = new RequestParams();
        params.addBodyParameter("tsId", SPHelper.getTSId());
        params.addBodyParameter("type", type);
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.GET_MANAGE_PRODUCT, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "get data response:" + responseInfo.result);
                if (TextUtils.isEmpty(responseInfo.result)) {
                    showMsg("您还没有发布任何商品");
                } else {
                    try {
                        JSONObject root = new JSONObject(responseInfo.result);
                        String code = root.getString("code");
                        if ("100".equals(code)) {
                            mLinear_noData.setVisibility(View.GONE);
                            list_goods.setVisibility(View.VISIBLE);
                            JSONArray list = root.getJSONArray("list");
                            mSaleSize = root.getInt("sizeSale") + "";
                            mStoreSize = root.getInt("sizeKuCun") + "";
                            text_warehouse_num.setText("(" + mStoreSize + ")");
                            text_shoping_num.setText("(" + mSaleSize + ")");

                            if (list.length() > 0) {
                                for (int i = 0; i < list.length(); i++) {
                                    JSONObject value = list.getJSONObject(i);
                                    ProductManageModel model = new ProductManageModel();
                                    model.setTpId(value.getInt("tpId"));
                                    model.setTpName(value.getString("tpName"));
                                    Double price = value.getDouble("tpSalePrice");
                                    model.setTpSalePrice("￥" + new DecimalFormat("0.00").format(price));
                                    String pic = value.getString("tpPic");
                                    if (pic.contains(",")) {
                                        model.setTpPic(pic.substring(0, pic.indexOf(",")));
                                    } else {
                                        model.setTpPic(pic);
                                    }
                                    model.setIsDiscount(value.getInt("tpIsDiscount") + "");
                                    Double discountPrice = value.getDouble("tpDiscountPrice");
                                    model.setDiscountPrice(new DecimalFormat("0.00").format(discountPrice));
                                    model.setTpRedCount(value.getInt("tpRedCount") + "");
                                    model.setTpStore(value.getInt("tpStore") + "");
                                    model.setIsDeduction(value.getInt("tpIsDeduction") + "");
                                    model.setTpSalesCount(value.getInt("tpSalesCount") + "");
                                    if ("1".equals(type)) {
                                        model.setStatus("1");
                                    } else {
                                        model.setStatus("0");
                                    }
                                    goodslist.add(model);
                                }
                                goodsManageAdapter.setData(goodslist);
                                goodsManageAdapter.notifyDataSetChanged();
                            } else {
                                mLinear_noData.setVisibility(View.VISIBLE);
                                list_goods.setVisibility(View.GONE);
                            }
                        } else {
                            String msg = root.getString("message");
                            showMsg(msg);
                            mLinear_noData.setVisibility(View.VISIBLE);
                            list_goods.setVisibility(View.GONE);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        showMsg("解析异常");
                        mLinear_noData.setVisibility(View.VISIBLE);
                        list_goods.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "get data failure:" + s);
                showMsg("当前网络不可用,请检查您的网络设置");
                mLinear_noData.setVisibility(View.VISIBLE);
            }
        });
    }
}
